<template>
  <div id="app">
    <div class="container">
      <div class="login-box">
        <div class="logo-box">
          <img src="@/assets/logo.png" alt="" />
        </div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              type="username"
              v-model="ruleForm.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              prefix-icon="el-icon-s-goods"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleForm)"
              >登录</el-button
            >
            <el-button class="el-button-rigth" @click="resetForm('ruleForm')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login.js'
export default {
  data () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          {
            min: 2,
            max: 6,
            message: '长度在 2 到 6 个字符',
            trigger: 'blur'
          },
          {
            required: true,
            pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/,
            message: '姓名不支持特殊字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},

  methods: {
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    async submitForm (value) {
      try {
        const res = await login(value)
        this.$store.commit('initUser', res.data.data)
        this.$router.push('/users')
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
#app,
html,
body {
  background-color: #2b4b6b;
  .container {
    height: 100%;
    width: 100%;
    .login-box {
      position: absolute;
      width: 450px;
      height: 300px;
      left: 50%;
      top: 50%;
      background-color: rgb(255, 255, 255);
      transform: translate(-50%, -50%);
      padding: 0 20px;
      box-sizing: border-box;
      .logo-box {
        width: 100%;
        position: absolute;
        top: 0;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        img {
          border-radius: 50%;
          width: 130px;
          height: 130px;
          background-color: #eee;
        }
      }
      .demo-ruleForm {
        margin-top: 115px;
        margin-left: 0;
        // .el-input__inner {
        //   width: 410px;
        // }
        /deep/.el-form-item__content {
          display: flex;
          justify-content: flex-end;
          .el-button-rigth {
            background-color: #909399;
            border-color: #909399;
            color: #fff;
          }
        }
      }
    }
  }
}
</style>
